package com.example.jussi.myproject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
 * Created by admin on 2018/11/29.
 */

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
    private Activity activity;
    private Context context;
    private List<CardBean> myCardList;

    public CardAdapter(Activity activity, Context context, List<CardBean> cardList) {
        this.activity = activity;
        this.context = context;
        myCardList = cardList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                CardBean card = myCardList.get(position);
                Intent intent = new Intent(context, ShowCard.class);
                intent.putExtra("card", card);
                context.startActivity(intent);
                activity.finish();
            }
        });
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                CardBean card = myCardList.get(position);
                SQLiteDatabase database = new MyDatabase(context).getWritableDatabase();
                String sql = "delete from cards where id = ?";
                database.execSQL(sql, new Object[]{card.getId()+""});
                Toast.makeText(context, "删除成功！！！", Toast.LENGTH_SHORT).show();
                activity.finish();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        CardBean card = myCardList.get(position);
        holder.companyName.setText(card.getBusinessName());
        holder.name.setText(card.getName());
    }

    @Override
    public int getItemCount() {
        return myCardList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView companyName, name;
        Button info, delete;
        View cardView;

        public ViewHolder(View itemView) {
            super(itemView);
            cardView = itemView;
            companyName = itemView.findViewById(R.id.itemCompanyName);
            name = itemView.findViewById(R.id.itemName);
            info = itemView.findViewById(R.id.itemInfo);
            delete = itemView.findViewById(R.id.itemDel);
        }
    }
}
